/**
 * @projectName ApexX-Cloud
 * @package com.apex.shared.mybatis.config
 * @className com.apex.shared.mybatis.config.StdoutLogger
 * @copyright Copyright 2020 Thunisoft, Inc All rights reserved.
 */
package com.apex.shared.mybatis.config;

import com.p6spy.engine.spy.appender.MessageFormattingStrategy;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * StdoutLogger
 *
 * @author MoNaiHui
 */
public class CustomMessageFormatter implements MessageFormattingStrategy {
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String s4) {
        if (!sql.trim().isEmpty()) {
            String sqlBegin = "============== SQL LOGGER BEGIN ==============";

            String sqlExecuteTime = "SQL 执行时间：" + this.format.format(new Date()) + "\n";
            String elapsedStr = "SQL 执行毫秒：" + elapsed + "ms" + "\n";
            String sqlPrint = "SQL 执行语句：" + "\r\n" + sql;
            String sqlEnd = "==============  SQL LOGGER END  ==============";

            return "\r\n" + sqlBegin + "\r\n" + sqlExecuteTime + elapsedStr + sqlPrint + "\r\n" + sqlEnd;
        }
        return "";
    }
}
